import { Entity, PrimaryGeneratedColumn, Column, Index } from "typeorm";
import { ManualTimestamp } from "../decorators/manual-timestamp.decorator";

/**
 * 系统消息类型枚举
 */
export enum SystemMessageType {
  PERSONAL = 1, // 个人消息
  BROADCAST = 2, // 广播消息
}

/**
 * 系统消息实体
 * 用于保存系统消息，包括个人消息和广播消息
 */
@Entity("system_messages")
@ManualTimestamp()
@Index(["senderId", "created_at"])
@Index(["receiverId", "created_at"])
@Index(["messageType", "created_at"])
@Index(["isRead", "created_at"])
export class SystemMessageEntity {
  @PrimaryGeneratedColumn({
    comment: "消息ID",
  })
  id!: number;

  @Column("int", {
    comment: "发送者ID，0表示系统",
    default: 0,
  })
  senderId!: number;

  @Column("varchar", {
    length: 100,
    comment: "发送者名称",
    default: "系统",
  })
  senderName!: string;

  @Column("text", {
    comment: "消息体，存储完整的消息数据格式",
  })
  messageBody!: string;

  @Column("tinyint", {
    comment: "消息类型：1-个人消息，2-广播消息",
  })
  messageType!: SystemMessageType;

  @Column("int", {
    nullable: true,
    comment: "接收者ID，广播消息可为空",
  })
  receiverId!: number | null;

  @Column("tinyint", {
    comment: "是否已读：0-未读，1-已读",
    default: 0,
  })
  isRead!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", {
    name: "updated_at",
    nullable: true,
    comment: "更新时间",
  })
  updated_at!: Date | null;
}
